package com.controller;

import com.pojo.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserLogInOutController {

    @Autowired
    private UserService userService;

    //1、登陆
    @PostMapping("/dologin.html")
    public String login(String code, String password, HttpServletRequest req){
        System.out.println("=========>进入controller");
        User user = userService.login(code, password);
        if (user != null) {
            req.getSession().setAttribute("userSession", user);
            return "redirect:/main.html";
        }
        req.setAttribute("error", "用户名或密码错误");
        return "forward:/login.jsp";
    }

    @RequestMapping("/main.html")
    public String toMain() {
        return "main";
    }

    //2.注销
    @RequestMapping("/dologout.html")
    public String logout(HttpServletRequest req){
        req.getSession().removeAttribute("userSession");
        return "redirect:/login.jsp";
    }
}
